package com.buluo.web.controller

import com.buluo.entity.Resource
import com.buluo.entity.Role
import com.buluo.service.ResourceService
import com.buluo.service.RoleService
import com.buluo.web.PKUtil
import com.m1905hd.common.global.JSONCode
import com.m1905hd.common.util.JSONUtil
import com.m1905hd.common.util.Page
import org.apache.shiro.authz.annotation.RequiresPermissions
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam

/**
 *
 * @author weiwei.huang
 * @email ihuangweiwei@163.com
 * @date 15/3/30
 */
@Controller
@RequestMapping("role")
class RoleController {


    @Autowired
    RoleService roleService

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @RequiresPermissions("role:view")
    def showList() {
        return "role/list"
    }

    @RequestMapping(value = "/list.json")
    @RequiresPermissions("role:view")
    def list(
            @RequestParam(defaultValue = "20") int limit,
            @RequestParam(defaultValue = "1") int pageIndex, String description, String name,
            @RequestParam(defaultValue = "-1") int available) {
        Page page = this.roleService.list([name: name, description: description, available: available], pageIndex, limit)
        [results: page.count, rows: page.data]
    }


    @RequestMapping(value = "listAll.json")
    def listAll() {
        List<Role> roleList = this.roleService.findAll()
    }

    @RequestMapping(value = "/edit.json")
    @RequiresPermissions("role:edit")
    def create(
            @RequestParam(defaultValue = "0") int id, String name, String description,
            @RequestParam(defaultValue = "false") boolean isNew) {
        Role role = new Role(name, description, 1)
        role.setId(id)
        role = (isNew == Boolean.TRUE ? this.roleService.create(role) : this.roleService.update(role))
        [code: JSONCode.SUCCESS.code]
    }

    @Autowired
    ResourceService resourceService

    @RequestMapping("/menu/{id}")
    @RequiresPermissions("role:edit")
    def toMenu(@PathVariable("id") int id, Model model) {
        List<Resource> resourceList = this.resourceService.findAll()
        Role role = this.roleService.find(id)
        model.addAttribute("data", JSONUtil.beanToJson(PKUtil.buildTree(resourceList, role.resourceIds)))

        model.addAttribute("checked", JSONUtil.beanToJson(role.resourceIds))
        model.addAttribute("roleId", id)
        return "role/menu"
    }


    @RequestMapping("bind/menu.json")
    @RequiresPermissions("role:edit")
    def bindMenu(int id, String ids) {
        def idsStr = ""
        if (!StringUtils.isEmpty(ids)) {
            if (ids.endsWith(",")) {
                idsStr = ids.substring(0, ids.length() - 1)
            }
        }
        this.roleService.bindMenu(id, idsStr);
        [code: JSONCode.SUCCESS.code]
    }
}
